package cn.aib.eduservice.service.impl;

import cn.aib.commonutils.R;
import cn.aib.eduservice.entity.EduCourse;
import cn.aib.eduservice.entity.EduCourseDescription;
import cn.aib.eduservice.entity.frontVo.CourseQueryVo;
import cn.aib.eduservice.entity.vo.CourseInfoVo;
import cn.aib.eduservice.entity.vo.CoursePublishVo;
import cn.aib.eduservice.entity.vo.CourseWebVo;
import cn.aib.eduservice.mapper.EduCourseMapper;
import cn.aib.eduservice.service.EduChapterService;
import cn.aib.eduservice.service.EduCourseDescriptionService;
import cn.aib.eduservice.service.EduCourseService;
import cn.aib.eduservice.service.EduVideoService;
import cn.aib.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-21
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService descriptionService;
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private EduChapterService chapterService;


    @Override
    @CacheEvict(value = "eduservice",key = "'selectCourselist'")
    public String addCourse(CourseInfoVo course) {
        /**
         *  1.将数据赋值到EduCourse和EduCourseDescription两个实体类中
         *  2.插入到对应的表，并建立表之间的关系，保证ID是相同的
         */
        //将数据赋值到EduCourse和EduCourseDescription两个实体类中
        //将course的属性赋值到EduCourse
        System.out.println(course);
        EduCourse eduCourse = new EduCourse();

        BeanUtils.copyProperties(course, eduCourse);

        System.out.println(eduCourse);

        //插入到数据库
        int result = baseMapper.insert(eduCourse);

        if(result <= 0){
            throw new GuliException(20001, "课程插入失败");
        }

        //将course的属性赋值到EduCourseDescription
        EduCourseDescription eduCourseDescription = new EduCourseDescription();

        BeanUtils.copyProperties(course, eduCourseDescription);

        //设置EduCourseDescription对象的ID和EduCourse一致
        eduCourseDescription.setId(eduCourse.getId());
        System.out.println(eduCourseDescription);
        //插入到数据库
        descriptionService.save(eduCourseDescription);

        return eduCourse.getId();
    }

    @Override
    //根据ID查询课程
    public CourseInfoVo getCourseInfoById(String courseId) {
        /**
         *  CourseInfoVo包含两部分信息：课程和课程描述
         *      单独查两张表
         */
        //查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);

        //查询课程描述表
        EduCourseDescription description = descriptionService.getById(courseId);

        //设置到CourseInfoVo
        CourseInfoVo courseInfoVo = new CourseInfoVo();

        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        BeanUtils.copyProperties(description,courseInfoVo);

        return courseInfoVo;
    }

    @Override
    @CacheEvict(value = "eduservice",key = "'selectCourselist'")
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        /**
         *  重新将数据设置到两种表中
         *      1.courseInfoVo数据复制到课程是实体类和课程描述实体类
         *      2.根据这两个实体类更新
         */
        //courseInfoVo数据复制到课程是实体类和课程描述实体类
        EduCourse eduCourse = new EduCourse();

        BeanUtils.copyProperties(courseInfoVo,eduCourse);

        int result = baseMapper.updateById(eduCourse);

        if(result <= 0){
            throw new GuliException(20001, "修改课程信息失败");
        }

        //根据这两个实体类更新
        EduCourseDescription description = new EduCourseDescription();

        BeanUtils.copyProperties(courseInfoVo,description);

        descriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo getPublishCourse(String courseId) {

        //查询课程信息
        CoursePublishVo publishCourse = baseMapper.getPublishCourse(courseId);

        return publishCourse;
    }

    @Override
    @CacheEvict(value = "eduservice",key = "'selectCourselist'")
    public R deleteCourse(String courseId) {
        //删除小节
        R rusult = videoService.removeVideoList(courseId);
        if(rusult.getCode() == 20001){
            throw new GuliException(20001,"视频批量删除失败");
        }

        //删除章节
        chapterService.removeChapterList(courseId);

        //删除课程简介
        descriptionService.removeById(courseId);

        //删除课程
        baseMapper.deleteById(courseId);

        return R.ok();
    }

    @Override
    @Cacheable(value = "eduservice",key = "'selectCourselist'")
    public List<EduCourse> selectCourselist(QueryWrapper<EduCourse> courseWrapper) {
        List<EduCourse> eduCourses = baseMapper.selectList(courseWrapper);

        return eduCourses;
    }

    @Override
    public Map<String, Object> selectFrontCoursePagelist(long current, long limit, CourseQueryVo courseQueryVo) {

        //创建page
        Page<EduCourse> page = new Page<>(current, limit);

        //判断并拼接条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(courseQueryVo.getSubjectParentId())){  //若一级分类不为空
            wrapper.eq("subject_parent_id", courseQueryVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseQueryVo.getSubjectId())){   //若二级分类不为空
            wrapper.eq("subject_id", courseQueryVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseQueryVo.getBuyCountSort())){   //若购买排序不为空
            wrapper.orderByDesc("buy_count");
        }
        System.out.println("price========="+courseQueryVo.getPriceSort());
        if(!StringUtils.isEmpty(courseQueryVo.getPriceSort())){   //若价格排序不为空
            wrapper.orderByDesc("price");
        }
        if(!StringUtils.isEmpty(courseQueryVo.getGmtCreateSort())){   //若创建排序不为空
            wrapper.orderByDesc("gmt_create");
        }
        //查询
        baseMapper.selectPage(page, wrapper);

        //将page的分页数据转移到map上

        List<EduCourse> records = page.getRecords();

        long currentPage = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("courseList", records);
        map.put("current", currentPage);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //返回
        return map;
    }

    @Override
    public CourseWebVo getFrontCourseInfo(String id) {
        CourseWebVo course = baseMapper.getFrontCourse(id);

        return course;
    }
}
